//
//  QuickCardViewCell.m
//  QRMaster
//
//  Created by myApple on 16/5/27.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "QuickCardViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIImageView+ImageSize.h"

@interface QuickCardViewCell()

@property(nonatomic,strong)UIView * topView;//16高度的view

@property(nonatomic,strong)UIImageView * backGroundImgView;

@property(nonatomic,strong)UIView * imageBottomView;//下面黑色背景

@property(nonatomic,strong)UILabel * roomNameLabel;

@property(nonatomic,strong)UIImageView * editImgView;//编辑

@property(nonatomic,strong)UIImageView * shareImgView;//分享

@property(nonatomic,strong)UIImageView * topImgView;//置顶

@property(nonatomic,strong)UIImageView * addRoomImgView;//没有收藏房间的时候的收藏图标

@property(nonatomic,strong)QuickCardModel * infoModel;

@end

@implementation QuickCardViewCell

#pragma mark - init
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * identityId = @"QuickCardViewCell";
    QuickCardViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identityId];
    if (!cell) {
        cell = [[QuickCardViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identityId];
        cell.backgroundColor = UIColorFromRGB(0xebebeb);
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self addAllSubView];
    }
    return self;
}

-(void)addAllSubView
{
    [self addSubview:self.topView];
    [self addSubview:self.backGroundImgView];
    [self.backGroundImgView addSubview:self.addRoomImgView];
    [self addSubview:self.imageBottomView];
    [self.imageBottomView addSubview:self.roomNameLabel];
    [self.imageBottomView addSubview:self.editImgView];
    [self.imageBottomView addSubview:self.shareImgView];
    [self.imageBottomView addSubview:self.topImgView];
    UITapGestureRecognizer * addRoomTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addRoom)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:addRoomTap];
    UITapGestureRecognizer * topTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noThing)];
    [self.topView addGestureRecognizer:topTap];
    
    UITapGestureRecognizer * bottomTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noThing)];
    [self.imageBottomView addGestureRecognizer:bottomTap];
}

-(void)noThing
{
    
}

#pragma mark - initData
-(void)setData:(QuickCardModel *)roomInfo
{
    self.infoModel = roomInfo;
    NSString * resourceString = @"defaultImageOne";
    switch (_index % 3) {
        case 0:
            resourceString = @"defaultImageOne";
            break;
        case 1:
            resourceString = @"defaultImageTwo";
            break;
        case 2:
            resourceString = @"defaultImageThree";
            break;
        default:
            resourceString = @"defaultImageOne";
            break;
    }
    NSString * imagePath = [[NSBundle mainBundle]pathForResource:resourceString ofType:@"jpg"];
    [self.backGroundImgView sd_setImageWithURL:[NSURL URLWithString:self.infoModel.imageURL] placeholderImage:[UIImage imageWithContentsOfFile:imagePath]];
//    [self.backGroundImgView sd_setImageWithURL:[NSURL URLWithString:self.infoModel.imageURL] placeholderImage:[UIImage imageWithContentsOfFile:imagePath] options:SDWebImageRetryFailed];
  
    self.roomNameLabel.text = self.infoModel.customerName;
    if ([self.infoModel.roomId isEqualToString:@""]) {
        self.imageBottomView.hidden = YES;
        self.addRoomImgView.hidden = NO;
    }
    else
    {
        self.imageBottomView.hidden = NO;
        self.addRoomImgView.hidden = YES;
    }
    
}

#pragma mark - layoutSubview
-(void)layoutSubviews
{
    float x = 0;
    float y = 8;
    float width = self.frame.size.width;
    float height = self.frame.size.height - y;
    self.backGroundImgView.frame = CGRectMake(x, y, width, height);
    
    self.topView.frame = CGRectMake(0, 0, width, 8);
    
    y = self.frame.size.height - 44;
    height = 44;
    self.imageBottomView.frame = CGRectMake(x, y, width, height);
    
    x = 16;
    y = (44 - 20)/2.0 - 10;
    width = self.frame.size.width / 2.0 - x;
    self.roomNameLabel.frame = CGRectMake(x, y, width, height);
    
    if (_index != 0) {
        x = self.frame.size.width - 44;
        y = 0;
        height = 44;
        width = 44;
        self.shareImgView.frame = CGRectMake(x, y, width, height);
        
        x = self.frame.size.width - 88;
        self.topImgView.frame = CGRectMake(x, y, width, height);
        self.topImgView.hidden = NO;
        
        x = self.frame.size.width - 132;
        self.editImgView.frame = CGRectMake(x, y, width, height);
    }
    else {
        
        x = self.frame.size.width - 44;
        y = 0;
        height = 44;
        width = 44;
        self.shareImgView.frame = CGRectMake(x, y, width, height);
        self.topImgView.hidden = YES;
        self.editImgView.frame = CGRectMake(self.frame.size.width - 88, y, width, height);
    }
    
    //添加房间图标
    x = (self.backGroundImgView.frame.size.width - 38)/2.0;
    y = (self.backGroundImgView.frame.size.height - 38)/2.0;
    width = 38;
    height = 38;
    self.addRoomImgView.frame = CGRectMake(x, y, width, height);
}


#pragma mark - lazy

-(UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc]init];
//        _topView.userInteractionEnabled = NO;
        _topView.backgroundColor = UIColorFromRGB(0xebebeb);
    }
    return _topView;
}

-(UIImageView *)backGroundImgView
{
    if (!_backGroundImgView) {
        _backGroundImgView = [[UIImageView alloc]init];
        _backGroundImgView.backgroundColor = [UIColor clearColor];
    }
    return _backGroundImgView;
}

-(UIImageView *)addRoomImgView
{
    if (!_addRoomImgView) {
        _addRoomImgView = [[UIImageView alloc]init];
        _addRoomImgView.image = [UIImage imageNamed:@"addQuickRoom"];
    }
    return _addRoomImgView;
}

-(UIView *)imageBottomView
{
    if (!_imageBottomView) {
        _imageBottomView = [[UIView alloc]init];
        _imageBottomView.backgroundColor = UIColorFromRGBAndA(0x000000, 0.6);
        _imageBottomView.userInteractionEnabled = YES;
    }
    return _imageBottomView;
}

-(UILabel *)roomNameLabel
{
    if (!_roomNameLabel) {
        _roomNameLabel = [[UILabel alloc]init];
        _roomNameLabel.textColor = UIColorFromRGB(0xffffff);
        _roomNameLabel.font = [UIFont systemFontOfSize:16.0];
    }
    return _roomNameLabel;
}

-(UIImageView *)editImgView
{
    if (!_editImgView) {
        _editImgView = [[UIImageView alloc]init];
        _editImgView.image = [UIImage imageNamed:@"editQuickCard"];
        _editImgView.userInteractionEnabled = YES;
        UITapGestureRecognizer * editTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editRoom)];
        _editImgView.edgeImage = CGRectMake(7, 12, 18, 20);
        [_editImgView addGestureRecognizer:editTap];
    }
    return _editImgView;
}

-(UIImageView *)shareImgView
{
    if (!_shareImgView) {
        _shareImgView = [[UIImageView alloc]init];
        _shareImgView.image = [UIImage imageNamed:@"shareQuickCard"];
        _shareImgView.userInteractionEnabled = YES;
        UITapGestureRecognizer * shareTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shareRoom)];
        _shareImgView.edgeImage = CGRectMake(5, 12, 18, 20);
        [_shareImgView addGestureRecognizer:shareTap];
    }
    return _shareImgView;
}

-(UIImageView *)topImgView
{
    if (!_topImgView) {
        _topImgView = [[UIImageView alloc]init];
        _topImgView.image = [UIImage imageNamed:@"upQuickCard"];
        _topImgView.userInteractionEnabled = YES;
        UITapGestureRecognizer * topTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topRoom)];
          _topImgView.edgeImage = CGRectMake(9, 12, 20, 19);
        [_topImgView addGestureRecognizer:topTap];
    }
    return _topImgView;
}

#pragma mark - 添加收藏房卡
-(void)addRoom
{
    if ([self.infoModel.roomId isEqualToString:@""]) {
        if (_delegate && [_delegate respondsToSelector:@selector(QuickCardViewCellAddNewRoom:)]) {
            [_delegate QuickCardViewCellAddNewRoom:self.infoModel.index];
        }
    }
    else
    {
        if (_delegate && [_delegate respondsToSelector:@selector(QuickCardViewCellGenerateCard:)]) {
            [_delegate QuickCardViewCellGenerateCard:self.infoModel];
        }
    }
}

#pragma mark - 有房卡的操作 编辑、分享、置顶
-(void)editRoom
{
    if (_delegate && [_delegate respondsToSelector:@selector(QuickCardViewCellEidt:)]) {
        [_delegate QuickCardViewCellEidt:self.infoModel];
    }
}

-(void)shareRoom
{
    if (_delegate && [_delegate respondsToSelector:@selector(QuickCardViewCellShare:)]) {
        [_delegate QuickCardViewCellShare:self.infoModel];
    }
}

-(void)topRoom
{
    if (_delegate && [_delegate respondsToSelector:@selector(QuickCardViewCellTop:)]) {
        [_delegate QuickCardViewCellTop:self.infoModel];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
